package com.carbeauty.logic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageCompare {
	
public static void main(String[] args)
{ 
	boolean same = new ImageCompare().compareImage(Constants.FILE_UPLOAD_DIR+"/444.jpg",Constants.FILE_UPLOAD_DIR+"/444.jpg");
	System.out.println(same);
}
	
	
/**
 * 配置比较宽度的像素点
 * @return
 */
public static int getWidthSize(){
	return 5;
}	
/**
 * 配置比较高度的像素点
 * @return
 */
public static int getHeightSize(){
	return 5;
}	
/**
 * 每个像素比较时 差值在多少范围识别为匹配
 * @return
 */
public static int getAbsInteger(){
	return 5;
}	

/**
 * 相似度大于多少时，识为图片为像素。
 * @return
 */
public static double getLev(){
	return 0.8;
}

/**
 * 获取图片的RGB值数组
 * @param in
 * @return
 */
public static String[][] getPX(InputStream in) {
		int[] rgb = new int[3];
		BufferedImage bi = null;
		try {
			bi = ImageIO.read(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int w1  = getWidthSize();
		int h1  = getHeightSize();
		int width = bi.getWidth();
		int height = bi.getHeight();
		int minx = bi.getMinX();
		int miny = bi.getMinY();
		String[][] list = new String[w1*2][h1*2];
		int c = 1;
		for (int i = width/2-w1; i < width/2+w1; i++) {
			for (int j = height/2-h1; j < height/2+h1; j++) {
				int pixel = bi.getRGB(i, j);
				rgb[0] = (pixel & 0xff0000) >> 16;
				rgb[1] = (pixel & 0xff00) >> 8;
				rgb[2] = (pixel & 0xff);
				list[i-(width/2-w1)][j-(height/2-h1)] = rgb[0] + "," + rgb[1] + "," + rgb[2];
				c++;
			}
		}
		rgb = null;
		return list;
	}
/**
 * 比较两个图片的是否相似
 * @param img1
 * @param img2
 * @return
 */
public   boolean compareImage(String img1, String img2) {
	File file  = new File(img1);
	try {
		FileInputStream fis = new FileInputStream(img1);
		FileInputStream fis2 = new FileInputStream(img2);
		return compareImage(fis,fis2);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return false;
}
public static boolean compareImage(InputStream img1, InputStream img2) {
		// 分析图片相似度 begin
		String[][] list1 = getPX(img1);
		String[][] list2 = getPX(img2);
		int xiangsi = 0;
		int busi = 0;
		int i = 0, j = 0;
		for (String[] strings : list1) {
			if ((i + 1) == list1.length) {
				continue;
			}
			for (int m = 0; m < strings.length; m++) {
				try {
					String[] value1 = list1[i][j].toString().split(",");
					String[] value2 = list2[i][j].toString().split(",");
					int k = 0;
					for (int n = 0; n < value2.length; n++) {
						if (Math.abs(Integer.parseInt(value1[k])
								- Integer.parseInt(value2[k])) <= getAbsInteger()) {
							xiangsi++;
						} else {
							busi++;
						}
					}
				} catch (RuntimeException e) {
					continue;
				}
				j++;
			}
			i++;
		}
		list1 = list2;
		list2 = list1;
		i = 0;
		j = 0;
		for (String[] strings : list1) {
			if ((i + 1) == list1.length) {
				continue;
			}
			for (int m = 0; m < strings.length; m++) {
				try {
					String[] value1 = list1[i][j].toString().split(",");
					String[] value2 = list2[i][j].toString().split(",");
					int k = 0;
					for (int n = 0; n < value2.length; n++) {
						if (Math.abs(Integer.parseInt(value1[k])
								- Integer.parseInt(value2[k])) <= getAbsInteger()) {
							xiangsi++;
						} else {
							busi++;
						}
					}
				} catch (RuntimeException e) {
					continue;
				}
				j++;
			}
			i++;
		}
		double lev = 0;
		try {
			lev = Double.parseDouble(xiangsi + "") / Double
					.parseDouble((busi + xiangsi) + "");
		} catch (Exception e) {
			lev = 0;
		}
		list1 = null;
		list2 = null;
		return lev>=getLev();
	}
}
